/**
 * @author Can-Chen
 * @description 入口类
 */

const { Parser } = require('acorn');
const NodeIterator = require('./iterator');
const Scope = require('./scope');

function Eval(code = '', extraDeclaration = {}) {

    /**
     * @desc 字符串执行文件
     */
    this.code = code;

    /**
     * @desc 传入参数 可选
     */
    this.extraDeclaration = extraDeclaration;

    /**
     * @desc ast树
     */
    this.ast = Parser.parse(code);
    /**
     * @desc 
     */
    this.nodeIterator = null;

    /**
     * @desc
     */
    this.init();
}

Eval.prototype = {

    ...Eval.prototype,

    init: function () {
        const globalScope = new Scope('function');

        Object.keys(this.extraDeclaration).forEach(key => {
            globalScope.addDeclaration(key, this.extraDeclaration[key])
        });

        this.nodeIterator = new NodeIterator(null, globalScope);
    },

    run: function () {

        return this.nodeIterator.traverse(this.ast);
    }
};

module.exports = Eval;